package com.bracketbird.client.gui.club.tournament.rtc;

import com.bracketbird.client.model.tournament.*;
import com.google.gwt.cell.client.*;
import com.google.gwt.safehtml.shared.*;
import com.google.gwt.user.cellview.client.*;
import com.google.gwt.view.client.*;

/**
 * 77
 */
public class RoundTable {

    LevelResultPanel parent;
    private Round round;

    Column<Match, String> resultCol;


    public RoundTable(LevelResultPanel parent, Round round) {
        this.round = round;
        this.parent = parent;
    }

    public CellTable getTable() {


        class MyCell extends EditTextCell {
            public void render(Context context, String value, SafeHtmlBuilder sb) {
                getViewData(context.getKey());


                if(!isEditing(context, parent.getElement(), "")){
                     sb.appendEscaped("o");

                }
                else{
                    super.render(context, value, sb);
                }

            }


        }



        // Create a CellTable.
        final CellTable<Match> table = new CellTable<Match>();



        // Create name column.
        TextColumn<Match> teamHomeCol = new TextColumn<Match>() {
            public String getValue(Match m) {
                return m.getTeamHome().getMostQualifiedTeamName();
            }
        };


        ListDataProvider l = new ListDataProvider();



        // Create name column.
        TextColumn<Match> teamOutCol = new TextColumn<Match>() {
            public String getValue(Match m) {
                return m.getTeamOut().getMostQualifiedTeamName();
            }
        };


        // .

        resultCol = new Column<Match, String>(new MyCell()) {
            @Override
            public String getValue(Match object) {
                return object.getName()+ "-";

            }
        };

        resultCol.setFieldUpdater(new FieldUpdater<Match, String>() {
            public void update(int index, Match m, String value) {

                String v = value.replace("-", "" );
                m.setName(v);
            }
        });


        // Add the columns.
        table.addColumn(teamHomeCol, "Home Team");
        table.addColumn(teamOutCol, "Out Team");
        table.addColumn(resultCol, "Result");
        // Set the total row count. This isn't strictly necessary, but it affects
        // paging calculations, so its good habit to keep the row count up to date.
        table.setRowCount(round.getMatches().size(), true);

        // Push the data into the widget.
        table.setRowData(0, round.getMatches());
        return table;
    }
}
